package com.yt.website.util;

import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

public class PinyinUtil {

    public PinyinUtil() {
    }

    public static String getPinyin(String chinese) {
        return getPinyinZh_CN(makeStringByStringSet(chinese));
    }

    public static String getPinyinToUpperCase(String chinese) {
        return getPinyinZh_CN(makeStringByStringSet(chinese)).toUpperCase();
    }

    public static String getPinyinToLowerCase(String chinese) {
        return getPinyinZh_CN(makeStringByStringSet(chinese)).toLowerCase();
    }

    public static String getPinyinFirstToUpperCase(String chinese) {
        return getPinyin(chinese);
    }

    public static String getPinyinJianPin(String chinese) {
        return getPinyinConvertJianPin(getPinyin(chinese));
    }

    public static String getPinyinJianPinToUpperCase(String chinese) {
        return getPinyinConvertJianPin(getPinyin(chinese)).split(",")[0];
    }

    private static Set makeStringByStringSet(String chinese) {
        char chars[] = chinese.toCharArray();
        if (chinese != null && !chinese.trim().equalsIgnoreCase("")) {
            char srcChar[] = chinese.toCharArray();
            String temp[][] = new String[chinese.length()][];
            for (int i = 0; i < srcChar.length; i++) {
                char c = srcChar[i];
                if (String.valueOf(c).matches("[\\u4E00-\\u9FA5]+"))
                    try {
                        temp[i] = PinyinHelper.toHanyuPinyinStringArray(chars[i], getDefaultOutputFormat());
                    } catch (BadHanyuPinyinOutputFormatCombination e) {
                        e.printStackTrace();
                    }
                else
                    temp[i] = (new String[]{String.valueOf(srcChar[i])});
            }

            String pingyinArray[] = Exchange(temp);
            Set pinYin = new HashSet();
            for (int i = 0; i < pingyinArray.length; i++)
                pinYin.add(pingyinArray[i]);

            return pinYin;
        } else {
            return null;
        }
    }

    private static HanyuPinyinOutputFormat getDefaultOutputFormat() {
        HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
        format.setCaseType(HanyuPinyinCaseType.LOWERCASE);
        format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        format.setVCharType(HanyuPinyinVCharType.WITH_U_AND_COLON);
        return format;
    }

    private static String[] Exchange(String strJaggedArray[][]) {
        String temp[][] = DoExchange(strJaggedArray);
        return temp[0];
    }

    private static String[][] DoExchange(String strJaggedArray[][]) {
        int len = strJaggedArray.length;
        if (len >= 2) {
            int len1 = strJaggedArray[0].length;
            int len2 = strJaggedArray[1].length;
            int newlen = len1 * len2;
            String temp[] = new String[newlen];
            int Index = 0;
            for (int i = 0; i < len1; i++) {
                for (int j = 0; j < len2; j++) {
                    temp[Index] = (new StringBuilder(String.valueOf(capitalize(strJaggedArray[0][i])))).append(capitalize(strJaggedArray[1][j]))
                            .toString();
                    Index++;
                }

            }

            String newArray[][] = new String[len - 1][];
            for (int i = 2; i < len; i++)
                newArray[i - 1] = strJaggedArray[i];

            newArray[0] = temp;
            return DoExchange(newArray);
        } else {
            return strJaggedArray;
        }
    }

    private static String capitalize(String s) {
        char ch[] = s.toCharArray();
        if (ch != null && ch.length > 0 && ch[0] >= 'a' && ch[0] <= 'z')
            ch[0] = (char) (ch[0] - 32);
        String newString = new String(ch);
        return newString;
    }

    private static String getPinyinZh_CN(Set stringSet) {
        StringBuilder str = new StringBuilder();
        int i = 0;
        for (Iterator iterator = stringSet.iterator(); iterator.hasNext(); ) {
            String s = (String) iterator.next();
            if (i == stringSet.size() - 1)
                str.append(s);
            else
                str.append((new StringBuilder(String.valueOf(s))).append(",").toString());
            i++;
        }

        return str.toString();
    }

    private static String getPinyinConvertJianPin(String chinese) {
        String strArray[] = chinese.split(",");
        String strChar = "";
        String as[];
        int k = (as = strArray).length;
        for (int j = 0; j < k; j++) {
            String str = as[j];
            char arr[] = str.toCharArray();
            for (int i = 0; i < arr.length; i++)
                if (arr[i] < '`' || arr[i] > '{')
                    strChar = (new StringBuilder(String.valueOf(strChar))).append(new String((new StringBuilder(String.valueOf(arr[i]))).toString()))
                            .toString();

            strChar = (new StringBuilder(String.valueOf(strChar))).append(",").toString();
        }

        if (strChar != null && !strChar.equals(""))
            strChar = strChar.substring(0, strChar.length() - 1);
        return strChar;
    }
}
